<?php
/**
 * 差旅费报销单
 * Created by PhpStorm.
 * User: 23783
 * Date: 2018/3/9
 * Time: 13:25
 */

namespace Model;


class TravelExpenseModel{
    protected $db;

    public function __construct()
    {
        global $DB;
        $this->db = $DB;
    }

    /**
     *
     * @param $document_id
     * @return bool|CDBResult
     */
    function GetWorkflowStateData($document_id){
        $sql ="SELECT  b.`ID`,b.`STARTED`,c.name AS USER_NAME,c.PERSONAL_MOBILE,a.`UF_DEPARTMENT`,d.name AS WORKFLOW_NAME,d.`IBLOCK_ID`,b.WORKFLOW_TEMPLATE_ID
            FROM `b_uts_user` AS a
            JOIN  `b_bp_workflow_state` AS b ON a.`VALUE_ID` = b.STARTED_BY
            JOIN  `b_user` AS c ON a.`VALUE_ID` = c.id
            JOIN `b_iblock_element` AS d  ON b.document_id= d.`ID`
            WHERE b.document_id=".$this->db->ForSql($document_id);
        return $this->db->Query($sql,true);
    }

    /**
     *
     * @param $iblock_id
     * @param $document_id
     * @return bool|\CDBResult
     */
    function GetWorkflowParameterData($iblock_id,$document_id){
        $iBlockSql ="SELECT a.`IBLOCK_ID`,a.`NAME`,a.`CODE`,b. `VALUE`
                     FROM `b_iblock_property` AS a
                     JOIN `b_iblock_element_property` AS b ON a.`ID`=b.`IBLOCK_PROPERTY_ID`
                     WHERE a.`IBLOCK_ID` = ".$this->db->ForSql($iblock_id)." AND `IBLOCK_ELEMENT_ID`=".$this->db->ForSql($document_id);
        return $this->db->Query($iBlockSql,true);
    }

    /**
     *
     * @param $workflow_id
     * @return bool|\CDBResult
     */
    function  GetTaskListData($workflow_id){
        $sql ="SELECT a.MODIFIED,c.name,a.`STATUS`,a.`IS_INLINE`
               FROM `b_bp_task` AS a
               JOIN `b_bp_task_user` AS b ON b.`TASK_ID`=a.`ID`
               JOIN `b_user` AS c ON c.id = b.`USER_ID`
               WHERE a.WORKFLOW_ID='".$this->db->ForSql($workflow_id)."'
               ORDER BY a.id ASC";
        return $this->db->Query($sql,true);
    }
}